#include "common.h"

CTetris::CTetris(void)
{
	m_currentDiamond = NULL;
	m_nextDiamond = NULL;
	m_diamondPool = new CDiamondPool();
	m_sink = new CSink();
	m_pause = FALSE;
}

CTetris::~CTetris(void)
{
}


BOOL CTetris::Init(std::string diamondFile)
{
	m_diamondPool.Init(diamondFile);
	m_sink.Init(diamondFile);
}

void CTetris::Start()
{
	m_currentDiamond = m_diamondPool.GetRndDiamond();
	m_nextDiamond = m_diamondPool.GetRndDiamond();
	m_currentDiamond->SetSink(&m_sink);

	// FallingThreadProc();
}

void CTetris::Pasue()
{
	m_pause = TRUE;
}

void CTetris::Resume()
{
	m_pause = TRUE;
}

void CTetris::Rotate()
{
	if (!m_pause)
	{
		if (!m_currentDiamond->Rotate())
		{
			m_currentDiamond->Reset();
			m_currentDiamond = m_nextDiamond;
			m_nextDiamond = m_diamondPool.GetRndDiamond();
			m_currentDiamond->SetSink(&m_sink);
		}
	}
}

void CTetris::Left()
{
	if (!m_pause)
	{
		if (!m_currentDiamond->Left())
		{
			m_currentDiamond->Reset();
			m_currentDiamond = m_nextDiamond;
			m_nextDiamond = m_diamondPool.GetRndDiamond();
			m_currentDiamond->SetSink(&m_sink);
		}
	}
}

void CTetris::Right()
{
	if (!m_pause)
	{
		if (!m_currentDiamond->Right())
		{
			m_currentDiamond->Reset();
			m_currentDiamond = m_nextDiamond;
			m_nextDiamond = m_diamondPool.GetRndDiamond();
			m_currentDiamond->SetSink(&m_sink);
		}
	}
}

void CTetris::Down()
{
	if (!m_pause)
	{
		if (!m_currentDiamond->Down())
		{
			m_currentDiamond->Reset();
			m_currentDiamond = m_nextDiamond;
			m_nextDiamond = m_diamondPool.GetRndDiamond();
			m_currentDiamond->SetSink(&m_sink);
		}
	}
}

void CTetris::Drop()
{
	if (!m_pause)
	{
		m_currentDiamond->Drop();
		m_currentDiamond->Reset();
		m_currentDiamond = m_nextDiamond;
		m_nextDiamond = m_diamondPool.GetRndDiamond();
		m_currentDiamond->SetSink(&m_sink);
	}
}

void CTetris::FallingThreadProc()
{
	for (;;)
	{
		Down();
		Sleep(500);
	}
}

ITetris* CreateTetris()
{
	return new CTetris;
}
